package com.system.util;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReflectUtil {
	public static void invokeMethods(Class clazz,HttpServletRequest request, HttpServletResponse response)
	{
		String name = request.getParameter("method");
		if (name == null || name.isEmpty()) {
			throw new RuntimeException("没有传递method参数,请给出你想调用的方法");
		}
		Class c = clazz;// 获得当前类的Class对象
		Method method = null;
		try {
			// 获得Method对象
			method = c.getMethod(name, HttpServletRequest.class, HttpServletResponse.class);
		} catch (Exception e) {
			throw new RuntimeException("没有找到" + name + "方法，请检查该方法是否存在");
		}
		try {
			method.invoke(clazz.newInstance(), request, response);// 反射调用方法
		} catch (Exception e) {
			System.out.println("错误栈");
			e.printStackTrace();
			System.out.println("你调用的方法" + name + ",内部发生了异常");
			throw new RuntimeException(e);
		}

	}
}
